# Project Description -------
#  Purpose: Create all Figures for:
#  "Pyrrhic Victory: How Territorial Wars Affect Political Attitudes and Behavior"
#  Last updated: "Thu Jun 23 13:38:52 2022"
#  Input data: "wave1_4_data.csv" and "voting_vis.csv"
#  Output: Figures 1-2 (main text) and Figure A2 (appendix)
#  sessionInfo():
#   R version 4.0.3 (2020-10-10)
#   Platform: x86_64-apple-darwin17.0 (64-bit)
#   Running under: macOS  12.1




# Load all relevant themes and packages-----

library("tidyverse")
library("xlsx")
library("DataCombine")
library("haven")
library("stargazer")
library("xtable")
library("texreg")
library("broom")
library("sandwich")
library("lmtest")
library("clubSandwich")
library("stargazer")
library("estimatr")
library("texreg")
library("scales")
library("RItools")
library("ggpubr")


# Main Text Figures------
##Figure 1-----

### Load INES data ----
main_data <- read.csv("data/wave1_4_data_vis.csv", na.strings=c("","NA"))
# recode post variable as numeric
main_data <- mutate(main_data,
                    post = ifelse(postwar == "Post-war", 1, 0))

# Transform turnout questions into numeric values
main_data <- mutate(main_data,
                    importanttovote = case_when(
                     importanttovote == "very important" ~ 1,
                     importanttovote == ".33333334" ~ .33333334,
                     importanttovote == ".66666669" ~ .66666669,
                     importanttovote == 0 ~ 0),
                    intendstovote = case_when(
                     intendstovote == "certainly yes" ~ 1,
                     intendstovote == ".33333334" ~ .33333334,
                     intendstovote == ".66666669" ~ .66666669,
                     intendstovote == 0 ~ 0))

###Estimate main models----

golda <- lm(ad_golda ~ post + 
             female + age + religiosity +
             hhincome + education + 
             borninisrael + originasiaafrica +
             maarach69 + likud69 + didntvote69 + indeplib69 + 
             religious69 + left69 + other69,
            data = main_data) %>% 
 tidy(., conf.int = T) %>% 
 filter(.,
        term == "post") %>% 
 mutate(.,
        term = case_when(
         term == "post" ~ "Prime Minister \nSupport"
        ))





dayan <- lm(ad_dayan ~ post + 
             female + age + religiosity +
             hhincome + education + 
             borninisrael + originasiaafrica+
             maarach69 + likud69 + didntvote69 + indeplib69 + 
             religious69 + left69 + other69,
            data = main_data) %>% 
 tidy(., conf.int = T) %>% 
 filter(.,
        term == "post") %>% 
 mutate(.,
        term = case_when(
         term == "post" ~ "Minister Defense \nSupport"
        ))



begin <- lm(ad_begin ~ post + 
             female + age + religiosity +
             hhincome + education + 
             borninisrael + originasiaafrica +
             maarach69 + likud69 + didntvote69 + indeplib69 + 
             religious69 + left69 + other69,
            data = main_data) %>% 
 tidy(., conf.int = T) %>% 
 filter(.,
        term == "post") %>% 
 mutate(.,
        term = case_when(
         term == "post" ~ "Opposition Leader \nSupport"
        ))





Labor <- lm(votemaarach ~ post + 
             female + age + religiosity +
             hhincome + education + 
             borninisrael + originasiaafrica +
             as.factor(survey) + 
             maarach69 + likud69 + didntvote69 + indeplib69 + 
             religious69 + left69 + other69,
            data = main_data) %>% 
 tidy(., conf.int = T) %>% 
 filter(.,
        term == "post") %>% 
 mutate(.,
        term = case_when(
         term == "post" ~ "Labor \nSupport"
        ))


Likud <- lm(votelikud ~ post + 
             post + 
             female + age + religiosity +
             hhincome + education + 
             borninisrael + originasiaafrica +
             as.factor(survey) + 
             maarach69 + likud69 + didntvote69 + indeplib69 + 
             religious69 + left69 + other69
            ,
            data = main_data) %>% 
 tidy(., conf.int = T) %>% 
 filter(.,
        term == "post") %>% 
 mutate(.,
        term = case_when(
         term == "post" ~ "Likud \nSupport"
        ))





### Plot estimated models-----

# bind 5 models
main_survey_coef <- rbind(Labor, golda, dayan, Likud, begin)


# Generate coefficient plot 
positions_main <- c("Prime Minister \nSupport", 
                    "Minister Defense \nSupport", 
                    "Labor \nSupport", "Opposition Leader \nSupport",
                    "Likud \nSupport")
ggplot(main_survey_coef, aes(x = term, y = estimate)) +
 geom_hline(yintercept = 0, color = "gray50", linetype = 2, size = 0.2) +
 geom_pointrange(aes(ymin = conf.low, ymax = conf.high), 
                 position = position_dodge(width = 0.4),
                 fill = "white", color = "dodgerblue4") +
 geom_text(aes(label= round(estimate, digits = 3)), hjust=-.55, vjust=-2.1, size = 2.5,
           family = "Palatino", colour = "maroon") +
 geom_text(aes(label= paste("(",round(std.error, digits = 3), ")")), hjust=-.33, vjust=-0.5, size = 2.5, 
           family = "Palatino", colour = "maroon") +
 scale_x_discrete(limits = positions_main)+ 
 ylim(-.35,.35) +
 labs(x = "",
      y = "Effect Size")+
 annotate("text", x = 1, y = -.35, label = "N=723", 
          size = 3, family = "Palatino", colour = "gray47") +
 annotate("text", x = 2, y = -.35, label = "N=723", 
          size = 3, family = "Palatino", colour = "gray47") +
 annotate("text", x = 3, y = -.35, label = "N=2123", 
          size = 3, family = "Palatino", colour = "gray47") +
 annotate("text", x = 4, y = -.35, label = "N=723", 
          size = 3, family = "Palatino", colour = "gray47") +
 annotate("text", x = 5, y = -.35, label = "N=2125", 
          size = 3, family = "Palatino", colour = "gray47") +
 # caption = "*Main effects of war, from linear regression models with demographic controls.") +
 theme(text = element_text(size = 9, family = "Palatino"),
       panel.grid.major = element_blank(), 
       axis.text.x = element_text(size = 7),
       plot.caption = element_text(size = 6, family = "Palatino",hjust = -.02),
       panel.grid.minor = element_blank(),
       panel.background = element_blank(), 
       axis.line = element_line(colour = "black"))










##Figure 2-----
###Load data------

voting_data <- read.csv("data/voting_vis.csv", na.strings=c("","NA"))

# convert treatment and post treatment variables to numeric
voting_data <- filter(voting_data,
                      urban == 1) %>% 
 mutate(.,
        TRT = case_when(
         TR == "Casuaties in 73'" ~ 1,
         TR == "No 73' casualties" ~ 0
        ),
        PTR = case_when(
         PT == "Pre-YK war" ~ 0,
         PT == "Post-YK war" ~ 1
        ))



###Estimate models-----

#Models likud
likud_pool <- lm_robust(rel_likud ~ TRT*PTR + terror_deaths + 
                         eligiblevoters + as.factor(year),
                        clusters = lcode,
                        se_type = "stata",
                        data = voting_data
) %>% 
 tidy(.) %>% 
 filter(.,
        term == "TRT:PTR") %>% 
 mutate(.,
        Model = "Pooled",
        term = "Likud")


likud_1973 <- voting_data %>% 
 filter(.,
        year != 1977) %>% 
 lm_robust(rel_likud ~ TRT*PTR + terror_deaths + 
            eligiblevoters,
           clusters = lcode,
           se_type = "stata",
           data = .
 )%>% 
 tidy(.) %>% 
 filter(.,
        term == "TRT:PTR") %>% 
 mutate(.,
        Model = "1973",
        term = "Likud")

likud_1977 <- voting_data %>% 
 filter(.,
        year != 1973) %>% 
 lm_robust(rel_likud ~ TRT*PTR + terror_deaths + 
            eligiblevoters,
           clusters = lcode,
           se_type = "stata",
           data = .
 )%>% 
 tidy(.) %>% 
 filter(.,
        term == "TRT:PTR") %>% 
 mutate(.,
        Model = "1977",
        term = "Likud")




# Models Labor
labor_pool <- lm_robust(rel_emet ~ TRT*PTR + terror_deaths + 
                         eligiblevoters + as.factor(year),
                        clusters = lcode,
                        se_type = "stata",
                        data = voting_data
) %>% 
 tidy(.) %>% 
 filter(.,
        term == "TRT:PTR") %>% 
 mutate(.,
        Model = "Pooled",
        term = "Labor")


labor_1973 <- voting_data %>% 
 filter(.,
        year != 1977) %>% 
 lm_robust(rel_emet ~ TRT*PTR + terror_deaths + 
            eligiblevoters,
           clusters = lcode,
           se_type = "stata",
           data = .
 )%>% 
 tidy(.) %>% 
 filter(.,
        term == "TRT:PTR") %>% 
 mutate(.,
        Model = "1973",
        term = "Labor")

labor_1977 <- voting_data %>% 
 filter(.,
        year != 1973) %>% 
 lm_robust(rel_emet ~ TRT*PTR + terror_deaths + 
            eligiblevoters,
           clusters = lcode,
           se_type = "stata",
           data = .
 )%>% 
 tidy(.) %>% 
 filter(.,
        term == "TRT:PTR") %>% 
 mutate(.,
        Model = "1977",
        term = "Labor")


### Plot main models-----

# bind models
voting_coef2 <- rbind(likud_pool, likud_1973, likud_1977,
                      labor_pool, labor_1973, labor_1977) 

positions_vot2 <- c("Labor", "Likud")
ggplot(voting_coef2, aes(x = term, y = estimate)) +
 geom_hline(yintercept = 0, color = "gray50", linetype = 2, size = 0.2) +
 geom_pointrange(aes(ymin = conf.low, ymax = conf.high, 
                     color = Model, shape = Model),
                 position = position_dodge(width = 0.4),
                 fill = "white") +
 
 #coord_flip()+
 labs(x = "",
      y = "Effect Size")+
 # caption = "*Difference-in-difference coefficients from linear regressions,\ncontrolling for locality-level elegible voters and terror fatalaties. \nStandard errors are clustered by locality.") +
 scale_x_discrete(limits = positions_vot2)+ 
 scale_color_manual(values = c("midnightblue", "maroon", "dodgerblue")) +
 scale_fill_manual(values = c("midnightblue", "maroon", "dodgerblue")) +
 theme(text = element_text(size = 8, family = "Palatino"),
       panel.grid.major = element_blank(), 
       axis.text.x = element_text(size = 6),
       plot.caption = element_text(size = 6, 
                                   family = "Palatino",hjust = -.02),
       panel.grid.minor = element_blank(),
       panel.background = element_blank(), 
       axis.line = element_line(colour = "black"),
       legend.key=element_blank())




# Appenidx Figures
## Figure A2------


# Likud parallel trends plot
lik_pt<- 
filter(voting_data, !is.na(TR)) %>% 
ggplot(., aes(year, rel_likud, color = TR)) +
 stat_summary(geom = 'point', size = 2) +
 stat_summary(geom = 'line', alpha = 0.2, linetype = "dashed") +
 scale_x_continuous(breaks = c(1965,1969,1973,1977)) +
 geom_vline(xintercept = 1972.8) +
 labs(y="Likud Vote Share",
      x = "Cycle",
      color = "Treatment") +
 scale_color_manual(values = c("dodgerblue", "maroon")) + 
 theme(text = element_text(size = 12, family = "Palatino"),
       panel.grid.major = element_blank(), 
       axis.text.x = element_text(size = 12),
       plot.caption = element_text(size = 12, 
                                   family = "Palatino",hjust = -.02),
       panel.grid.minor = element_blank(),
       panel.background = element_blank(), 
       axis.line = element_line(colour = "black"),
       legend.key=element_blank())



# Labor parallel trends plot


em_pt <- 
 filter(voting_data, !is.na(TR)) %>% 
 ggplot(., aes(year, rel_emet, color = TR)) +
 stat_summary(geom = 'point', size =2) +
 stat_summary(geom = 'line', alpha = 0.2, linetype = "dashed") +
 scale_x_continuous(breaks = c(1965,1969,1973,1977)) +
 geom_vline(xintercept = 1972.8) +
 labs(y="Labor Vote Share",
      x = "Cycle",
      color = "Treatment") +
 scale_color_manual(values = c("dodgerblue", "maroon")) + 
 theme(text = element_text(size = 12, family = "Palatino"),
       panel.grid.major = element_blank(), 
       axis.text.x = element_text(size = 12),
       plot.caption = element_text(size = 12, 
                                   family = "Palatino",hjust = -.02),
       panel.grid.minor = element_blank(),
       panel.background = element_blank(), 
       axis.line = element_line(colour = "black"),
       legend.key=element_blank())



# Combine Labor and Likud plots
ggarrange(em_pt, lik_pt,  align = "hv", common.legend = TRUE, legend = "bottom")

